package com.chinaedu.oc.serv.saledate.impl;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.chinaedu.oc.constant.Constant;
import com.chinaedu.oc.dao.commondity.ICommodityDao;
import com.chinaedu.oc.dao.saledate.IDescDao;
import com.chinaedu.oc.dao.saledate.IPictureDao;
import com.chinaedu.oc.dao.saledate.ISaledateDao;
import com.chinaedu.oc.dao.saledate.IVideoDao;
import com.chinaedu.oc.po.commodity.Commodity;
import com.chinaedu.oc.po.saledate.Desc;
import com.chinaedu.oc.po.saledate.Picture;
import com.chinaedu.oc.po.saledate.Saledate;
import com.chinaedu.oc.po.saledate.Videos;
import com.chinaedu.oc.serv.saledate.ISaledateServ;
import com.chinaedu.oc.util.common.IdCreater;


@Service
public class SaledateServImpl implements ISaledateServ {

	
	@Autowired
	private ISaledateDao saledateDao;
	
	@Autowired
	private IPictureDao pictureDao;
	
	@Autowired
	private IDescDao descDao;
	
	@Autowired
	private IVideoDao videoDao;
	
	@Autowired
	private ICommodityDao iCommodityDao;
	
	
	/**
	 * 增加销售日期
	 */
	@Override
	public void insertOrUpdate(List<Saledate> saledates,String commodityId) {
		// TODO Auto-generated method stub
		//先删除所有该商品的销售期
		saledateDao.deleteByCommodityId(commodityId);
		//如果增加新的销售期则给新的销售期生成ID
		for(int i = 0; i < saledates.size(); i ++) {
			if(StringUtils.isEmpty(saledates.get(i).getId())) {
				saledates.get(i).setId(IdCreater.getId());
				saledates.get(i).setCommodityId(commodityId);
				saledates.get(i).setCreateDate(new Timestamp(System.currentTimeMillis()));
			}else {
				saledates.get(i).setCommodityId(commodityId);
				saledates.get(i).setCreateDate(new Timestamp(System.currentTimeMillis()));
			}
		}
		Commodity commodity = new Commodity();
		commodity.setId(commodityId);
		//添加销售期
		if(saledates.size() > 0) {
			saledateDao.insert(saledates);
			commodity.setIsHasSaledate(Constant.COMMODITY_IS_HAS_SALEDATE_A);
		}else {
			commodity.setIsHasSaledate(Constant.COMMODITY_IS_HAS_SALEDATE_B);
		}
		//修改商品的销售期
		iCommodityDao.updateByCommodity(commodity);
	}
	
	/**
	 * 根据销售日期ID先将改销售期内的所有描述删掉，再从新增加描述信息
	 */
	@Override
	public void insertOrUpdateMsg(Saledate saledate) {
		// TODO Auto-generated method stub
		
		pictureDao.deleteBySaledateId(saledate.getId());
		descDao.deleteBySaledateId(saledate.getId());
		videoDao.deleteBySaledateId(saledate.getId());
		
		if(saledate.getPictureList() != null) {
			List<Picture> pictureList = saledate.getPictureList();
			for (int i = 0; i < pictureList.size(); i ++) {
				pictureList.get(i).setId(IdCreater.getId());
				pictureList.get(i).setSaledateId(saledate.getId());
			}
			if(saledate.getPictureList().size() > 0) {
				pictureDao.insert(saledate.getPictureList());
			}
			
		}
		
		if(saledate.getVideosList() != null) {
			List<Videos> videosList = saledate.getVideosList();
			for (int i = 0; i < videosList.size(); i ++) {
				videosList.get(i).setId(IdCreater.getId());
				videosList.get(i).setSaledateId(saledate.getId());
			}
			if(saledate.getVideosList().size() > 0) {
				videoDao.insert(saledate.getVideosList());
			}
			
		}
		
		if(saledate.getDescList() != null) {
			List<Desc> descList = saledate.getDescList();
			for (int i = 0; i < descList.size(); i ++) {
				descList.get(i).setId(IdCreater.getId());
				descList.get(i).setSaledateId(saledate.getId());
			}
			if(saledate.getDescList().size() > 0) {
				descDao.insert(saledate.getDescList());
			}
		}
	}
	

	@Override
	public Saledate queryBySaleId(String id) {
		// TODO Auto-generated method stub
		return saledateDao.queryBySaleId(id);
	}


	@Override
	public void deleteBySaledateId(String saldateId) {
		// TODO Auto-generated method stub
		saledateDao.delete(saldateId);
	}

	@Override
	public void deleteByCommodityId(String commodityId) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<Saledate> findSaledateByCommodityId(String commodityId) {
		// TODO Auto-generated method stub
		Saledate saledate = new Saledate();
		saledate.setCommodityId(commodityId);
		List<Saledate> saledates = saledateDao.findSaledateByCase(saledate);
		System.out.println(saledates);
		if(saledates.size() == 0) {
			return null;
		}else {
			return saledates; 
		}
	}

	@Override
	public List<Saledate> findSaledateByCase(Saledate saledate) {
		// TODO Auto-generated method stub
		List<Saledate> findSaledateByCase = saledateDao.findSaledateByCase(saledate);
		return findSaledateByCase;
	}
	
	


}
